<?php

	error_reporting(-1);
	
	/*
	78632, 'Николай Присяжнюк'
	78842, 'Рынок хлеба'
	80494, 'Рынок муки'
	80508, 'Рынок зерна'
	84372, 'Аграрный рынок Украины'
	
	{"search_id":"82328","media_type":"17"} 
	
	0  News
	1  Blogs/Forums
	2  Microblogs (Twitter/Qip)
	17 Social Networks
	18 Presentations
	19 Video
	27 Job listings
	28 E-Shops
	*/
	
	$API_KEY = 'd2e0cddae164f3ab6238849b716ff776';
	if(isset($_GET['search_id'])){
		$search_id_array = array($_GET['search_id']);
	}else{
		$search_id_array = array('78632','78842','80494','80508','84372');
	}
	$id_admin = 10;
	
	foreach($search_id_array as $search_id){
	
		$search_id_slug = urlencode('{"search_id":"'.$search_id.'"}');
			
		$URI = 'http://dashboard.semanticforce.net/webservices/api_ui.php?api_key='.$API_KEY.'&m_name=doDownloadXML&m_param='.$search_id_slug;
		
		$curl = curl_init($URI);
		$fp =fopen('files/semantic.zip','w');
		curl_setopt($curl, CURLOPT_FILE, $fp);
		curl_setopt($curl, CURLOPT_HEADER, 0);
		curl_exec($curl);
		curl_close($curl);
		fclose($fp);

		$zip = zip_open("files/semantic.zip");
		
		$output = '';
		
		if ($zip) {
			//echo "Все пучком!<br />";
			while ($zip_entry = zip_read($zip)) {
				echo "Название:         " . zip_entry_name($zip_entry) . "<br />\n";
				echo "Исходный размер:  " . zip_entry_filesize($zip_entry) . "<br />\n";
				echo "Сжатый размер:    " . zip_entry_compressedsize($zip_entry) . "<br />\n";
				echo "Метод сжатия:     " . zip_entry_compressionmethod($zip_entry) . "<br />\n";

				if (zip_entry_open($zip, $zip_entry, "r")) {
					//echo "Содержимое файла:\n";
					$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
					//echo "$buf\n";
					$output .= $buf;
					
					zip_entry_close($zip_entry);
				}
				echo "\n";

			}

			zip_close($zip);
		}else{
			echo "Все пропало!<br />";
		}
		
		// Подключаем базу данных
		require_once("godb.php");

        $config = array(
            'host'      => 'localhost',
            'username'  => 'u_zakupkid',
            'passwd'    => 'gPPq5ys6',
            'dbname'    => 'zakupkidev',
            'charset'   => 'utf8',
            'debugtime' => false,
            'debug'     => false
        );

		goDB::makeDB($config);
		
		//$file_handle = fopen("test.xml", "r");
		//$output = @fread($file_handle,filesize("test.xml")); 
		//fclose($file_handle); 
			
		try {
			parse_xml($output, $id_admin);
		} catch (Exception $e) {
			echo 'Выброшено исключение: ',  $e->getMessage(), "\n";
		}
	}
	
	function parse_xml($xmlstr, $id_admin){
		
		if (strlen($xmlstr)>10000000){
			error_parse("error 100! Количество символов превышает 10000000.\n");
		}
		
		if(empty($xmlstr)){
			error_parse("error 101! Пустое сообщение.\n");
		}
		
		if (($sXML = @simplexml_load_string($xmlstr))===false){
			error_parse("error 102! Ошибка чтения XML. Ваши данные повреждены. Проверте структуру XML-файла.\n");
		}
		
		//$xml = new SimpleXMLElement($sXml, LIBXML_NOCDATA);
		
		$params = $sXML->children();
		
		$category 	= $params->search_name;//substr($params->search_name, 0, 20);
		$category_sl= url_title(strtolower($category));
		$facets 	= $params->facets->children();
		$keywords 	= $facets->keywords->children();
		$items 		= $params->items->children();
		
		$count_insrted_items = 0;
		$count_all_items = 0;
		
		$query = "
				SELECT 
					blog_categories.id AS `id`
				FROM
					{blog_categories} AS blog_categories
				WHERE
					blog_categories.slug = ?:slug
		";
		$parameters_query = array('slug' => $category_sl
									);
		$id_categorie = goDB::queryDB($query,$parameters_query, 'el', 's0x_');
		
		if(!$id_categorie){
			$query = "
					INSERT
					INTO {blog_categories}
						(slug, title)
					VALUES
						(?:slug , ?:title)
			";
			$parameters_query = array('slug' => $category_sl,
									  'title' => $category
										);
			$id_categorie = goDB::queryDB($query,$parameters_query, 'id', 's0x_');
				
		}
			
		foreach ($items as $item){
			
			$id_item = (int)$item->id;
			$title = $item->title;//substr($item->title, 0, 100)
			$title_slug = ''.(string)$id_item.'-'.url_title(strtolower($title));
			$source = $item->source;
			$link = $item->link;
			$pubDateShort = $item->pubDateShort;
			$description = str_replace('\n', '<br />', $item->description);
			$fte = str_replace('\n', '<br />', $item->fte->p);
			$fte = ($fte == '') ? $description : $fte;
			
			$query = "
					SELECT *
					FROM
						{blog_semantic} AS blog_semantic
					WHERE
						blog_semantic.s_IdSemantic = ?i:id_semantic
			";
			$parameters_query = array('id_semantic' => $id_item
										);
			$result = goDB::queryDB($query,$parameters_query, 'assoc', 's0x_');
				
			$count_all_items++;
			
			if(sizeof($result)>0){
				continue;
			}
			
			if (!empty($pubDateShort))
			{
				$array_pubDateShort = explode('.', (string)$pubDateShort);
				
				date_default_timezone_set('Europe/Kiev');
				
				$created_on = mktime(0,0,0,(int)$array_pubDateShort[1],(int)$array_pubDateShort[0],(int)$array_pubDateShort[2]); 
			
			}

			else
			{
				$created_on = now();
			}
			
			$query = "
					INSERT 
					INTO {blog}
						(title, slug, category_id, attachment, intro, body, author_id, created_on, comments_enabled, status)
					VALUE
						(?:title, ?:slug, ?i:category_id, ?:attachment, ?:intro, ?:body, ?i:id_admin, ?i:created_on, FALSE, 'live')
			";
			$parameters_query = array('title' => $title,
									'title' => $title,
									'slug' => $title_slug,
									'category_id' => $id_categorie,
									'attachment' => '',
									'intro' => '<p>'.$description.'</p>',
									'body' => '<p>'.$fte.'</p>
									<hr>
									<div class="meta"><div class="source"><span class="lighter">Источник:</span> <a href="'.$link.'" target="blank">'.$source.'</a></div></div>
									',
									'created_on' => $created_on,
									'id_admin' => $id_admin
										);
			
			$id_blog = goDB::queryDB($query,$parameters_query, 'id', 's0x_');
			
			$query = "
					INSERT 
					INTO {blog_semantic}
						(s_IdBlog, s_IdSemantic)
					VALUE
						(?i:s_IdBlog, ?i:s_IdSemantic)
			";
			$parameters_query = array('s_IdBlog' => $id_blog,
										's_IdSemantic' => $id_item
										);
			
			$id_semantic = goDB::queryDB($query,$parameters_query, 'id', 's0x_');
			
			$count_insrted_items++;
			
			//foreach ($item as $key => $node){
				
			//	echo $key.' - '.$node.'<br />';
			//}
		}
		
		echo 'Было добавлено '.$count_insrted_items.' новостей из '.$count_all_items.' загружаемых';
		//$arr = $sXML->attributes();  
		
		
	}

	function error_parse($Msg){
		//echo $Msg;
		throw new Exception($Msg);
		exit();
	}

	function url_title($str, $separator = 'dash', $lowercase = FALSE)
    {
		$config = array();
		
        $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
		
        $foreign_characters = array(
            '/ä|æ|ǽ/' => 'ae',
            '/ö|œ/' => 'oe',
            '/ü/' => 'ue',
            '/Ä/' => 'Ae',
            '/Ü/' => 'Ue',
            '/Ö/' => 'Oe',
            '/À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ|А/' => 'A',
            '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª|а/' => 'a',
            '/Б/' => 'B',
            '/б/' => 'b',
            '/Ç|Ć|Ĉ|Ċ|Č|Ц/' => 'C',
            '/ç|ć|ĉ|ċ|č|ц/' => 'c',
            '/Ð|Ď|Đ|Д/' => 'D',
            '/ð|ď|đ|д/' => 'd',
            '/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě|Е|Ё|Э/' => 'E',
            '/è|é|ê|ë|ē|ĕ|ė|ę|ě|е|ё|э/' => 'e',
            '/Ф/' => 'F',
            '/ф/' => 'f',
            '/Ĝ|Ğ|Ġ|Ģ|Г/' => 'G',
            '/ĝ|ğ|ġ|ģ|г/' => 'g',
            '/Ĥ|Ħ|Х/' => 'H',
            '/ĥ|ħ|х/' => 'h',
            '/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ|И/' => 'I',
            '/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı|и/' => 'i',
            '/Ĵ|Й/' => 'J',
            '/ĵ|й/' => 'j',
            '/Ķ|К/' => 'K',
            '/ķ|к/' => 'k',
            '/Ĺ|Ļ|Ľ|Ŀ|Ł|Л/' => 'L',
            '/ĺ|ļ|ľ|ŀ|ł|л/' => 'l',
            '/М/' => 'M',
            '/м/' => 'm',
            '/Ñ|Ń|Ņ|Ň|Н/' => 'N',
            '/ñ|ń|ņ|ň|ŉ|н/' => 'n',
            '/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ|О/' => 'O',
            '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º|о/' => 'o',
            '/П/' => 'P',
            '/п/' => 'p',
            '/Ŕ|Ŗ|Ř|Р/' => 'R',
            '/ŕ|ŗ|ř|р/' => 'r',
            '/Ś|Ŝ|Ş|Š|С/' => 'S',
            '/ś|ŝ|ş|š|ſ|с/' => 's',
            '/Ţ|Ť|Ŧ|Т/' => 'T',
            '/ţ|ť|ŧ|т/' => 't',
            '/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ|У/' => 'U',
            '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ|у/' => 'u',
            '/В/' => 'V',
            '/в/' => 'v',
            '/Ý|Ÿ|Ŷ|Ы/' => 'Y',
            '/ý|ÿ|ŷ|ы/' => 'y',
            '/Ŵ/' => 'W',
            '/ŵ/' => 'w',
            '/Ź|Ż|Ž|З/' => 'Z',
            '/ź|ż|ž|з/' => 'z',
            '/Æ|Ǽ/' => 'AE',
            '/ß/'=> 'ss',
            '/Ĳ/' => 'IJ',
            '/ĳ/' => 'ij',
            '/Œ/' => 'OE',
            '/ƒ/' => 'f',
            '/Ч/' => 'Ch',
            '/ч/' => 'ch',
            '/Ю/' => 'Ju',
            '/ю/' => 'ju',
            '/Я/' => 'Ja',
            '/я/' => 'ja',
            '/Ш/' => 'Sh',
            '/ш/' => 'sh',
            '/Щ/' => 'Shch',
            '/щ/' => 'shch',
            '/Ж/' => 'Zh',
            '/ж/' => 'zh'
        );

        $str = preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str);
        
        $replace = ($separator == 'dash') ? '-' : '_';
        
        $trans = array(
            '&\#\d+?;'                => '',
            '&\S+?;'                => '',
            '\s+'                    => $replace,
            '[^a-z0-9\-\._]' => '',
            $replace.'+'            => $replace,
            $replace.'$'            => $replace,
            '^'.$replace            => $replace,
            '\.+$'                    => ''
        );

        $str = strip_tags($str);

        foreach ($trans as $key => $val)
        {
            $str = preg_replace("#".$key."#i", $val, $str);
        }
        
        if ($lowercase === TRUE)
        {
            //if( function_exists('mb_convert_case') )
            //{
            //    $str = mb_convert_case($str, MB_CASE_LOWER, "UTF-8");
            //}
            //else
            //{
                $str = strtolower($str);
            //}
        }

        $str = preg_replace('#[^'.$config['permitted_uri_chars'].']#i', '', $str);
        
        return trim(stripslashes($str)); 
    }
 
?>